package cn.doctorpda.conference.popup;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.webkit.WebView;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.adapter.ShareDialogAdapter;
import cn.doctorpda.conference.utils.ShareUtils;
import cn.doctorpda.conference.view.callbackview.OnItemClickListener;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.renren.Renren;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qq.QQClientNotExistException;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.tencent.weibo.TencentWeibo;
import cn.sharesdk.wechat.favorite.WechatFavorite;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by huangjinlong on 2016/5/17.
 */
public class ShareDialog extends DialogFragment {

    private RecyclerView mRecyclerView;

    private String title;
    private String text;
    private String url;
    private String thumb;
    private ShareDialogAdapter adapter;

    public static ShareDialog newInstance(String title, String text, String url, String thumb) {
        ShareDialog dialog = new ShareDialog();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("text", text);
        args.putString("url", url);
        args.putString("thumb", thumb);
        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        title = args.getString("title");
        text = args.getString("text");
        url = args.getString("url");
        thumb = args.getString("thumb");
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
//        Dialog dialog = super.onCreateDialog(savedInstanceState);
//        dialog.setTitle(getActivity().getString(R.string.share));
//        Window window = dialog.getWindow();
//        window.setGravity(Gravity.BOTTOM);
//        window.setWindowAnimations(R.style.PopupAnimation);

        View view = getActivity().getLayoutInflater().inflate(R.layout.popup_share, null);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        mRecyclerView.setLayoutManager(manager);
        adapter = new ShareDialogAdapter(getActivity());
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void setOnItemClick(int position, Object data) {
                toShare(position, data);
            }
        });
        return new AlertDialog.Builder(getActivity())
                .setView(view)
                .create();
    }

    private void toShare(int position, Object data) {
        String platform = "";
//        if (position == 0) {
//            platform = SinaWeibo.NAME;
//        } else
        if (position == 0) {
            platform = Wechat.NAME;
        } else if (position == 1) {
            platform = QQ.NAME;
        } else if (position == 2) {
            platform = WechatMoments.NAME;
        } else if (position == 3) {
            platform = WechatFavorite.NAME;
        } else if (position == 4) {
            platform = QZone.NAME;
        } else if (position == 5) {
            platform = TencentWeibo.NAME;
        } else if (position == 6) {
            platform = Renren.NAME;
        }
        ShareUtils.showShare(getActivity(), platform, false, title, text, thumb, url);
    }

//    @Override
//    public void onStart() {
//        super.onStart();
//        DisplayMetrics dm = new DisplayMetrics();
//        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
//        getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
//    }
}
